// 网关配置的前缀
var blogServer = "/blogSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量
var searchValue = "";

let Authorization = "";
let refreshToken = "";

let userId = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        blogServer = data.blogServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    getCommentInfo(searchValue, currentPage, pageSize);
}

function getCommentInfo(searchValue, currentPage, pageSize) {
    $.ajax({
        type: "get",
        async: false,
        url: blogServer + "/blog/selectCommentByUser",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            searchValue: searchValue,
            userId: userId,
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function (data) {
            console.log("所有的评论信息 - 第 " + currentPage + " 页");
            let iPage = data.datas;
            if (iPage === null || iPage === ""){
                return;
            }
            total = iPage.total;
            pageSize = iPage.size;
            pages = iPage.pages;
            //清空数据
            $("#commentContent").empty();
            //返回的数据用data.rows获取内容
            $.each(iPage.records, function (i, item) {
                $("#commentContent").append(
                    "<tr class='job-application-item'>" +
                    "<td class='application-title'>" +
                    "<span> " + item.userName + " </span>" +
                    "</td>" +
                    "<td class='application-review'>" +
                    "<span> " + item.content + " </span>" +
                    "</td>" +
                    "<td class='application-job'>" +
                    "<h3><a>" + item.createDate + "</a></h3>" +
                    "</td>" +
                    "<td class='delete-application text-right'>" +
                    "<a onclick='deleteComment(\"" + item.commentId + "\")'><i class='lnr lnr-trash'></i></a>" +
                    "</td>" +
                    "</tr>"
                );

            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getCommentInfo(searchValue, num, size);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    });
}

//  搜索框
function click_search() {
    searchValue = $('#reviews_search').val();
    getCommentInfo(searchValue, 1, pageSize);
}

function deleteComment(commentId) {
    $.confirm({
        title: '提示',
        content: '确认删除该评论吗？',
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-warning',
                action: function () {
                    $.ajax({
                        type: "DELETE",
                        async: false,
                        url: blogServer + "/blog/commentInfo?commentId=" + commentId,
                        headers: {
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        success: function (data) {
                            // 成功
                            cocoMessage.info(1000, data.resp_msg, function () {
                            });
                            location.reload();
                        },
                        error: function (err) {
                            cocoMessage.error(2000, err.statusText, function () {
                                console.log(err);
                            });
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}
